package com.hzncc.flowable_diboot.service.impl;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.core.vo.JsonResult;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.entity.DingDingGroup;
import com.hzncc.flowable_diboot.mapper.DingDingGroupMapper;
import com.hzncc.flowable_diboot.service.DingDingGroupService;
import com.hzncc.flowable_diboot.service.DingtalkService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 钉钉群聊 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-07-10
 * Copyright © MyCorp
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DingDingGroupServiceImpl extends BaseServiceImpl<DingDingGroupMapper, DingDingGroup> implements DingDingGroupService {

    private final DingtalkService dingtalkService;

    /**
     * 创建钉钉群聊
     *
     * @param group
     * @param users
     * @throws Exception
     */
    @Override
    public JsonResult<?> createDingGroup(DingDingGroup group, List<String> users) throws Exception {
        JsonResult<String> token = dingtalkService.getToken(IamSecurityUtils.getCurrentTenantId());
        if (token.getCode() != 0){
            return token;
        }
        //JsonResult<?> chat = dingtalkService.createChat(token.getData(),group, users);
        JsonResult<String> chat = dingtalkService.createTemplateChat(token.getData(),group);
        if (chat.getCode() != 0) {
            return chat;
        }
        createEntity(group);
        return JsonResult.OK("创建成功，请到钉钉中查看，并将【华中CRM】机器人添加到群中，以便接收日报");
    }
}